package app.auto.runner.base.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;

public class MeasuredListView extends ListView {
    public MeasuredListView(Context context) {
        super(context);
    }

    public MeasuredListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MeasuredListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int hms = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, hms);
    }

    public static void setHegiht(ListView listView,int orig) {        /*获取adapter*/
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }
        int totalHeight = 0;        /* listAdapter.getCount()返回数据项的数目*/
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0); // 计算子项View 的宽高			totalHeight += listItem.getMeasuredHeight(); // 统计所有子项的总高度		}				/*		 * listView.getDividerHeight()获取子项间分隔符占用的高度		 * params.height最后得到整个ListView完整显示需要的高度		 * */		ViewGroup.LayoutParams params = listView.getLayoutParams();		params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));		listView.setLayoutParams(params);	}
        }

    }
}
